var mapJSON = "data/world-countries.json";
var locationJSON = "data/data_lp.json"
var streamJSON = null;
var map = null;
var scaleSlider = null;
var monthIndicator = true;
var startTimestamp = "2012/5";
var globeIndicator = false;
var kpi = "USD";

$(document).ready(function() {
	alert(getStandardDeviation([20,32,210,300,480,800,1400,1700,5100,14000]));
	//drawMap(mapJSON,locationJSON,streamJSON,lon,lat,x,y,scale,globeIndicator)
	var mapObject = new drawMap(mapJSON,locationJSON,streamJSON,9.0,48.5,611,478,270,globeIndicator,monthIndicator,startTimestamp); 
	map = mapObject.svg;

	//scenario click handler	
	var scenarioclick = function(event){
		if(event.target.innerHTML == "Hedging") {
			locationJSON = ["data/data_hedging_company.json","data/data_hedging_banks.json"];
			streamJSON = "data/datastreams_hedging.json";
			monthIndicator = false;
			startTimestamp = "2012";
			d3.json(streamJSON, function(collection) {
				createBarchart("timeline",collection.data,streamJSON,map,projection);
				createPiechart("piechart",streamJSON,timestamp,kpi);
				$("#chartkpi").html(kpi);
				//create kpi header
				var kpilabel = $(document.createElement('div')).attr({
					"id": "kpilabel",
					"class" : "filterseplabel"
				}).html("KPIs");
				
				var filtersep = $(document.createElement('div')).attr({
					"id": "kpifiltersep",
					"class" : "filtersep"
				});
		
				$("#area_right").append(kpilabel);
				$("#area_right").append(filtersep);
				
				//create kpi click handler
				var kpiclick = function(event){
					kpi = event.target.innerHTML;
					d3.json(streamJSON, function(collection) {
						createBarchart("timeline",collection.data,streamJSON,map,projection);
						createPiechart("piechart",streamJSON,timestamp,kpi);
						$("#chartkpi").html(kpi);	
					});	
					$("#map").empty();
					mapObject = new drawMap(mapJSON,locationJSON,streamJSON,9.0,48.5,611,478,270,globeIndicator,monthIndicator,startTimestamp); 
					map = mapObject.svg;	
				};
				
			    //create kpi filter
				var kpiValues = ["AUD","CAD","CYN","EUR","INR","RUB","SGD","USD","ZAR"];
				$("#area_right").append(createFilterLabel("Currency",kpiValues.length,"currencyLabel"));
				var kpiSelectMenu = createSelectMenu("selectkpi",kpi,kpiValues,kpiclick);
				$("#area_right").append(kpiSelectMenu);
			});	
		} else if(event.target.innerHTML = "Liquidity Planning") {
			locationJSON = "data/data_lp.json";
			streamJSON = null;
			monthIndicator = true;
			startTimestamp = "2012/5";
			$("#kpifiltersep").remove();
			$("#kpilabel").remove();
			$("#selectkpi").remove();
			$("#currencyLabel").remove();
			d3.json(locationJSON, function(collection) {
				createBarchart("timeline",collection.data,locationJSON,map,projection);
				createPiechart("piechart",locationJSON,timestamp);
				$("#chartkpi").html("Liquidity");	
			});	
		}
		$("#map").empty();
		mapObject = new drawMap(mapJSON,locationJSON,streamJSON,9.0,48.5,611,478,270,globeIndicator,monthIndicator,startTimestamp); 
		map = mapObject.svg;	
	}
	//create scenario filter
	var scenarioValues = ["Liquidity Planning","Hedging"];
	$("#area_right").append(createFilterLabel("Scenario"));
	var scenarioSelectMenu = createSelectMenu("selectscenario","Liquidity Planning",scenarioValues,scenarioclick);
	$("#area_right").append(scenarioSelectMenu);
	
	//create geographic header
	var geolabel = $(document.createElement('div')).attr({
		"class" : "filterseplabel"
	}).html("Geographic");
	
	var filtersep = $(document.createElement('div')).attr({
		"class" : "filtersep"
	});
	
	$("#area_right").append(geolabel);
	$("#area_right").append(filtersep);

    //create region filter
	var regionValues = ["- All Regions -","APJ","EMEA","LATIN AMERICA","NORTH AMERICA"];
	$("#area_right").append(createFilterLabel("Region",regionValues.length-1));
	var regionSelectMenu = createSelectMenu("selectregion","- Select Region -",regionValues);
	$("#area_right").append(regionSelectMenu);

	//create country filter
	d3.json(mapJSON, function(collection) {
		var countryValues = ["- All Countries -"];
		collection.features.forEach(function(o) {
			countryValues.push(o.properties.name);
		});
		countryValues.sort();
		var countrySelectMenu = createSelectMenu("selectcountry","- Select Country -",countryValues);
		$("#area_right").append(createFilterLabel("Country",countryValues.length-1));
		$("#area_right").append(countrySelectMenu);
		
		//create location filter
		d3.json(locationJSON, function(collection) {
			var locationValues = ["- All Locations -"];
			collection.data.forEach(function(o) {
				if(!searchArrayValue(locationValues,o.properties.name))
					locationValues.push(o.properties.name);
			});
			var locationSelectMenu = createSelectMenu("selectlocation","- Select Location -",locationValues);
			$("#area_right").append(createFilterLabel("Location",locationValues.length-1));
			$("#area_right").append(locationSelectMenu);
			
			//create timeline
			createBarchart("timeline",collection.data,locationJSON,map,projection);
			createPiechart("piechart",locationJSON,timestamp);
			
			//displayed kpi
			$("#chartkpi").html(capitaliseFirstLetter(collection.data[0].properties.attribute));
		});	
	});	
	
	//click handler for move circle
	$("#movetop").click(function(){
		mapObject.move("y",30)
	});
	
	$("#movebottom").click(function(){
		mapObject.move("y",-30)
	});
	
	$("#moveleft").click(function(){
		mapObject.move("x",30)
	});
	
	$("#moveright").click(function(){
		mapObject.move("x",-30)
	});
	
	$("#movecenter").click(function(){
		mapObject.move("xy",[611,478])
	});
	
	//create zoom slider
	scaleSlider = $("#scale").slider({
		  orientation: "vertical",
		  min: 300,
		  max: 3500*20,
		  value: 1743,
		  slide: function(event, ui) {
			  mapObject.scale(ui.value);
		  }
		});
});

function toggleProjection(){
	$("#map").empty();
	
	if($("#toggle").attr("class") == "world"){
		$("#toggle").attr({
			"src" : "img/globe.png",
			"class":"flat"
		});
		globeIndicator = false;
		mapObject = new drawMap(mapJSON,locationJSON,streamJSON,9.0,48.5,611,478,270,globeIndicator,monthIndicator);
		map = mapObject.svg;
	} else {
		$("#toggle").attr({
			"src" : "img/map.png",
			"class":"world"
		});
		globeIndicator = true;
		mapObject = new drawMap(mapJSON,locationJSON,streamJSON,9.0,48.5,611,478,270,globeIndicator,monthIndicator);
		map = mapObject.svg;	
	}
	$(".chart").remove();
	if($("#selectscenario").children().get(0).innerHTML == "Liquidity Planning") {
		d3.json(locationJSON, function(collection) {
			createBarchart("timeline",collection.data,locationJSON,map,projection);
			createPiechart("piechart",locationJSON,timestamp);
			$("#chartkpi").html("Liquidity Planning");	
		});	
	} else {
		d3.json(streamJSON, function(collection) {
			createBarchart("timeline",collection.data,streamJSON,map,projection);
			createPiechart("piechart",streamJSON,timestamp,kpi);
			$("#chartkpi").html(kpi);	
		});	
	}
}